package com.liaoye.hellocustomview.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

public class PaintView2 extends View {

    private static final String TAG = PaintView2.class.getSimpleName();

    public PaintView2(Context context) {
        super(context);
    }

    public PaintView2(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public PaintView2(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //设置画笔的基本属性
        Paint paint=new Paint();

        //设置填充样式
        paint.setStyle(Paint.Style.FILL);

        //画基线
        int baseLineX = 0;
        int baseLineY = 200;
        String text = "how are you ?";

        //设置画笔颜色
        paint.setColor(Color.RED);
        canvas.drawLine(baseLineX, baseLineY, 3000, baseLineY, paint);

        //画文字
        paint.setColor(Color.GREEN);
        paint.setTextSize(100);
        canvas.drawText(text, baseLineX, baseLineY, paint);


        Paint.FontMetrics fontMetrics = paint.getFontMetrics();

        Paint.FontMetricsInt fontMetricsInt = paint.getFontMetricsInt();

        int descentY = baseLineY + fontMetricsInt.descent;
        int topY = baseLineY + fontMetricsInt.top;
        int ascentY = baseLineY + fontMetricsInt.ascent;
        int bottomY = baseLineY + fontMetricsInt.bottom;

        //绘制最顶线
        paint.setColor(Color.BLUE);
        canvas.drawLine(baseLineX, topY, 3000, topY, paint);

        //绘制顶线
        paint.setColor(Color.YELLOW);
        canvas.drawLine(baseLineX, ascentY, 3000, ascentY, paint);

        //绘制底线
        paint.setColor(Color.CYAN);
        canvas.drawLine(baseLineX, descentY, 3000, descentY, paint);

        //绘制最底线
        paint.setColor(Color.MAGENTA);
        canvas.drawLine(baseLineX, bottomY, 3000, bottomY, paint);


        //文字矩形的高度
        int height = bottomY - topY;

        float width = paint.measureText(text);



        Rect rect = new Rect();
        paint.getTextBounds(text, 0, text.length(), rect);

        //获取的矩形的位置是基于 (0,0)基线获取的矩形区域 因此真正的 矩形区域的点要想乡下平移

        int minTop = rect.top + baseLineY;
        int minBottom = rect.bottom + baseLineY;

        Log.i(TAG, "onDraw: width= " + width + " height=" + height + " minTop= " + minTop + " minBottom= " + minBottom);
    }
}
